<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 新订单日志
 *
 * admin-NewOrderLog
 *
 * @Apidoc\Title("新订单日志")
 * @Apidoc\Group("api")
 */
class NewOrderLog extends BaseController
{
    /**
     * 新增
     *
     * admin-NewOrderLog-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\NewOrderLog::create($data['new_order_log']);
            return success();
        } else {
            $options = [];
            $new_order_log__new_orders_id_option = \app\common\model\NewOrders::column('name as name, id as id');
            $options['new_order_log__new_orders_id_options'] = $new_order_log__new_orders_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-NewOrderLog-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\NewOrderLog::update($data['new_order_log']);
            return success();
        } else {
            $data = \app\common\model\NewOrderLog::find(input('new_order_log__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['new_order_log__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $new_order_log__new_orders_id_option = \app\common\model\NewOrders::column('name as name, id as id');
            $options['new_order_log__new_orders_id_options'] = $new_order_log__new_orders_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-NewOrderLog-delete
     */
    public function delete()
    {
        \app\common\model\NewOrderLog::destroy(input('new_order_log__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-NewOrderLog-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_new_order_log');
        $query->join('db_new_orders', 'db_new_orders.id = db_new_order_log.new_orders_id');
        if (input('new_order_log__name', '') !== '') {
            $query->where('db_new_order_log.name', 'like', '%' . input('new_order_log__name', '', 'search_in') . '%');
        }
        if (input('new_orders__id', '') !== '') {
            $query->where('db_new_orders.id', '=', input('new_orders__id', '', 'search_in'));
        }
        $query->where('db_new_order_log.delete_time', '=', null);
        $query->where('db_new_orders.delete_time', '=', null);
        $query->field(['db_new_order_log.id' => 'new_order_log__id']);
        $query->field(['db_new_order_log.name' => 'new_order_log__name']);
        $query->field(['db_new_orders.name' => 'new_orders__name']);
        $query->order('db_new_order_log.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $new_orders__id_option = \app\common\model\NewOrders::column('name as name, id as id');
        $options['new_orders__id_options'] = $new_orders__id_option;
        View::assign($options);
        return View::fetch();
    }
}